package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee){

       //1.根据username查询数据库
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lqw);
        if(emp==null){
            return R.error("登录失败");
        }
        //2.对页面提交的密码进行md5加密处理,并与数据库中的进行比较
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());//使用md5加密易破解
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }
        //3.比对成功，确定是否被禁用
        if(emp.getStatus()==0){
            return R.error("员工已禁用");
        }
        //4.用户状态正常。登录成功
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出登录成功");
    }


    /**
     * 新增员工信息
     * @param employee
     * @return
     */
    @PostMapping
    public R<String>  addEmployee(@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("添加用户信息成功");
    }

    /**
     * 分页模糊查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
  @GetMapping("/page")
    public R pageQuery(int page,int pageSize,String name){
        Page<Employee> pageInfor=new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
        lqw.like(name!=null,Employee::getName,name);
        employeeService.page(pageInfor, lqw);
       return R.success(pageInfor);
  }

    /**
     * 数据回显
     * @param id
     * @return
     */
   @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
       Employee employee = employeeService.getById(id);
       if(employee!=null){
           return R.success(employee);
       }
       return R.error("没有查询到对应员工信息");
   }

    /**
     *修改员工信息
     * @param employee
     * @return
     */
   @PutMapping
    public R<String> updateEmployee(@RequestBody Employee employee){
       long id = Thread.currentThread().getId();
       log.info("线程id为：{}",id);
       employeeService.updateById(employee);
       return R.success("更新员工信息成功");
   }
}
